var ctx = window.daijia.ctx;

var offset = 0;
var limit = 20;
var pagingLimit = 20;//显示分页时的条数
var currentTotalElements = 0;//当前显示总条数


var pageLoading = true;//是否能滚动加载
var hasShowPages = false;//是否已显示分页
var firstLoadPages = true;//分页初始化时 不自动加载

//筛选条件
var search_couponCode = null;
var search_phone = null;
var search_id = $("#couponRuleId").val();


$(document).ready(function () {


    initdata(false);
    $("#searchButton").click(function () {
        search_couponCode = $("#couponCode").val();
        search_phone = $("#phone").val();

        offset = 0;
        initdata(false);
    });


    $(document).on("keydown", "body", function () {
        if (window.event.keyCode == 13) {
            $("#searchButton").click();
        }
    });

    $("#clear_time").click(function () {
        $("#couponCode").val("");
        $("#phone").val("");
        search_couponCode = "";
        search_phone = "";
        offset = 0;
        initdata(false);
    });

    //滚动
    $(window).scroll(function () {
        //$(window).scrollTop()这个方法是当前滚动条滚动的距离
        //$(window).height()获取当前窗体的高度
        //$(document).height()获取当前文档的高度
        var bot = 0; //bot是底部距离的高度
        if ((bot + $(window).scrollTop()) >= ($(document).height() - $(window).height())) {
            //当底部基本距离+滚动的高度〉=文档的高度-窗体的高度时；
            if (pageLoading) {
                pageLoading = false;
                offset += limit;
                initdata(true);
            }
        }
    });

});


function initdata(isappend) {


    $.ajax({
        url: ctx + '/couponrule/couponData',
        type: 'GET',
        async: false,
        dataType: 'json',
        data: {
            "id": search_id,
            "couponCode": search_couponCode,
            "phone": search_phone,
            "pager.offset": offset,
            "limit": limit,
        },
        success: function (result) {
            var newcontent = '';
            var totalElements = result.totalElements;
            $("#NoData").text("");
            if (totalElements > 0) {
                $(result.content).each(function (index, data) {

                    var statusColor = "";
                    var mainButton = "";
                    var selectButton = "";
                    if (data.status != 3 && data.status != 2) {
                        statusColor = "btn-primary";
                        mainButton = "<a href='javascript:;' data-value='" + data.id + "' class='btn btn-primary btn-xs invalidthis'>失效</a>";
                    }
                    var allButton = "<div class='btn-group'>" +
                        mainButton +
                        "</div>";

                    var codeKey;
                    if (data.codeKey) {
                        if (main) {
                            codeKey = "<td>" + data.codeKey + "</td>";
                        } else {
                            codeKey = "<td>**************</td>";
                        }
                    } else {
                        codeKey = "<td>-</td>";
                    }

                    var status;
                    if (data.status == 0) {
                        status = "<td>未绑定</td>";
                    } else if (data.status == 1) {
                        status = "<td><span style='color:#EE00EE;font-weight:700;'>未使用</span></td>";
                    } else if (data.status == 2) {
                        status = "<td><span style='color:red;font-weight:700;'>已使用</span></td>";
                    } else if (data.status == 3) {
                        status = "<td><span style='color:#8B2252;font-weight:700;'>已失效</span></td>";
                    }
                    var passenger;
                    if (data.passenger) {
                        passenger = "<td>" + data.passenger.name + "/" + data.passenger.phone + "</td>";
                    } else if (data.phone) {
                        passenger = "<td>-/" + data.phone + "</td>";
                    } else {
                        passenger = "<td>-</td>";
                    }
                    var type;
                    if (data.passenger) {
                        if (data.passenger.type == 0) {
                            type = "<td>个人用户</td>";
                        } else {
                            type = "<td>企业用户</td>";
                        }
                    } else {
                        type = "<td>-</td>";
                    }

                    newcontent += "<tr class='grid-data tr-hui' style='display:none;'>" +
                        "<td>" + data.code + "</td>" +
                        codeKey +
                        status +
                        passenger +
                        type +
                        "<td>" + data.createdStr + "</td>" +
                        // "<td>" + data.lineName + "</td>" +
                        // "<td>" + data.couponPrice + "</td>" +
                        "<td>" + allButton + "</td>" +
                        "</tr>";
                });
            } else {
                $("#NoData").text("当前页面暂无记录 ");
            }

//        	hideLoading();

            if (!isappend) {
                $(".table tbody .grid-data").remove();
            }
            $(".table tbody").append(newcontent);

            $(".table tbody .grid-data").each(function (index, element) {
                $(element).fadeIn(200 + (index * 35));
            });


            if (!hasShowPages) {
                if (!isappend) {
                    currentTotalElements = result.content.length;
                    pageLoading = true;
                } else {
                    if (currentTotalElements < totalElements) {
                        currentTotalElements += result.content.length;
                        pageLoading = true;
                    } else {
                        pageLoading = false;
                    }
                }
            }

            if (hasShowPages) {
                firstLoadPages = true;
                $(".pageshow li a").html("共" + totalElements + " 条数据");
                $(".pagedata").paging(totalElements, {
                    format: '[< ncnnn >]',
                    perpage: pagingLimit,
                    page: (offset / limit) + 1,
                    onSelect: function (page) {
                        limit = pagingLimit;
                        offset = (page - 1) * limit;
                        if (!firstLoadPages) {
                            initdata(false);
                        }
                        firstLoadPages = false;
                        return false;
                    },
                    onFormat: function (type) {
                        switch (type) {
                            case 'block': // n and c
                                if (!this.active)
                                    return '';
                                else if (this.value != this.page)
                                    return '<li><a href="#">' + this.value + '</a></li>';
                                return '<li class="active"><a href="#">' + this.value + '</a></li>';
                            case 'next': // >
                                return '<li><a href="#">&rsaquo;</a><li>';
                            case 'prev': // <
                                return '<li><a href="#">&lsaquo;</a><li>';
                            case 'first': // [
                                return '<li><a href="#">&laquo;</a><li>';
                            case 'last': // ]
                                return '<li><a href="#">&raquo;</a></li>';
                        }
                    }
                });

            } else {
                //滚动达到pagingLimit条开始显示分页
                if (currentTotalElements == pagingLimit) {

                    pageLoading = false;
                    hasShowPages = true;

                    $(".pageshow").show();
                    $(".pagedata").show();
                    $(".pageshow li a").html("共" + totalElements + " 条数据");
                    $(".pagedata").paging(totalElements, {
                        format: '[< ncnnn >]',
                        perpage: pagingLimit,
                        onSelect: function (page) {
                            limit = pagingLimit;
                            offset = (page - 1) * limit;
                            if (!firstLoadPages) {
                                initdata(false);
                            }
                            firstLoadPages = false;
                            return false;
                        },
                        onFormat: function (type) {
                            switch (type) {
                                case 'block': // n and c
                                    if (!this.active)
                                        return '';
                                    else if (this.value != this.page)
                                        return '<li><a href="#">' + this.value + '</a></li>';
                                    return '<li class="active"><a href="#">' + this.value + '</a></li>';
                                case 'next': // >
                                    return '<li><a href="#">&rsaquo;</a><li>';
                                case 'prev': // <
                                    return '<li><a href="#">&lsaquo;</a><li>';
                                case 'first': // [
                                    return '<li><a href="#">&laquo;</a><li>';
                                case 'last': // ]
                                    return '<li><a href="#">&raquo;</a></li>';
                            }
                        }
                    });

                }
            }

        }
    });

}

function showLoading() {
    if ($('#loadblacklayer').length == 0) {
        var dw = $(".table").width();
        var dh = $(".table").height();
        var backlayer = "<div id='loadblacklayer'><div style='margin-top:" + (dh / 2) + "px;margin-left:" + (dw / 2) + "px;'><img src='" + ctx + "/assets/css/images/loading.gif' /></div></div>"
        $(".table tbody").append(backlayer);
        $('#loadblacklayer').css({
            "z-index": "9",
            "background": "#000",
            "opacity": "0.5",
            "position": 'absolute',
            "top": 0,
            "width": dw,
            "height": dh
        });
    }
}

function hideLoading() {
    if ($('#loadblacklayer').length > 0) {
        $('#loadblacklayer').remove();
    }
}

